<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.wan888888.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Model\MenuModel;
use ReflectionMethod;
use Think\Controller;


/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class IndexController extends AdminController {

    /**
     * 后台首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        $this->meta_title = '管理首页';
        $this->display();
    }


    /**
     * 删除菜单
     * @param $id
     */
    /*public function menudel($id)
    {
        $model = new MenuModel();
        $model->where(['id' => $id])->delete();

        $rows = $model->field('id')->where(['pid' => $id])->select();
        $ids = array_column($rows, 'id');
        if ($ids) {
            $model->where(['pid' => ['IN', $ids]])->delete();
            $model->where(['id' => ['IN', $ids]])->delete();
        }
    }*/

    /**
     * 导入菜单
     * @throws \ReflectionException
     */
    public function menu()
    {
        $menus = [];

        $it = new \DirectoryIterator(__DIR__ . '/');
        foreach ($it as $v) {
            $file = $v->getFilename();
            if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
                require_once $file;
                $controller = substr($file, 0, -10);
                $menu = $this->dumpMenu($controller);
                $menu and $menus = array_merge($menus, $menu);
            }
        }

        $menu_ids = [];
        foreach ($menus as $menu) {
            $path = $menu[0];
            array_shift($menu);
            $splits = explode('/', $path);
            if (count($splits) < 2) continue;

            if (isset($menu_ids[$splits[0]])) {
                $parent = $menu_ids[$splits[0]];
            } else {
                $parent = $this->saveMenu(0, $splits[0], $menu[1], '');
                $menu_ids[$splits[0]] = $parent;
            }

            if (count($splits) == 2) {
                $menu_ids[$path] = $this->saveMenu($parent, $splits[1], $menu[1], $menu[0]);
            } else if (count($splits) == 3) {
                $pid = $menu_ids[$splits[0] . '/' . $splits[1]];
                $this->saveMenu($pid, $splits[2], $menu[1], $menu[0]);
            }
        }

        $this->ajaxReturn($menus);
    }


    private function saveMenu($pid, $name, $url, $group)
    {
        $model = new MenuModel();
        $row = $model->where(['title' => $name, 'pid' => $pid])->find();
        if ($row) {
            $row['group'] = $group;
            $row['url'] = $url;
            $row['hide'] = $pid == 0 ? 0 : ($group ? 0 : 1);
            $model->save($row);
            return $row['id'];
        }
        $insert = ['title' => $name, 'pid' => $pid, 'url' => $url, 'group' => $group, 'hide' => $pid == 0 ? 0 : ($group ? 0 : 1), 'status' => 1];
        $model->add($insert);
        return $model->getLastInsID();
    }


    /**
     * @param $controller
     * @return array
     * @throws \ReflectionException
     */
    private function dumpMenu($controller)
    {
        $rc = new \ReflectionClass('Admin\Controller\\' . $controller);
        $retval = [];
        foreach ($rc->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
            $doc = $method->getDocComment();
            if (!$doc) continue;
            preg_match('#@menu\(([^\)]+)\)#', $doc, $matches);
            if (!$matches) continue;
            list($path, $group) = explode(',', trim($matches[1]));

            $action = $method->getName();
            $c = substr($controller, 0, -10);
            $url = "$c/$action";
            $retval[] = [$path, trim($group), $url];
        }

        ksort($retval);
        return $retval;
    }
}
